package com.itheima.controller;

import com.itheima.domain.Option;
import com.itheima.domain.Pow;
import com.itheima.domain.Teacher;
import com.itheima.domain.User;
import com.itheima.util.AgeUtil;

import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);

    private static User adminuser;

    public TeacherController(User adminuser) {
        this.adminuser = adminuser;
    }

    //客服功能入口
    public void start() throws IOException {
        teacher:
        while (true) {
            System.out.println("********欢迎来到教师管理系统********");
            System.out.println("1. 添加教师" + (adminuser.getPow().compareTo(Pow.TEACHER) == 0 ? "  (锁定!)" : ""));
            System.out.println("2. 删除教师" + (adminuser.getPow().compareTo(Pow.TEACHER) == 0 ? "  (锁定!)" : ""));
            System.out.println("3. 修改教师" + (adminuser.getPow().compareTo(Pow.TEACHER) == 0 ? "  (锁定!)" : ""));
            System.out.println("4. 查看教师");
            System.out.println("5. 退出");

            //2. 用户录入选项,匹配对应功能
            String option = sc.next();
            if (adminuser.getPow().compareTo(Pow.TEACHER) == 0) {
                if (!option.equals("4") && !option.equals("5")) {
                    System.out.println("权限不足,无法执行此操作");
                    continue;
                }
            }
            switch (option) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    delectTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用教师管理系统");
                    break teacher;
                default:
                    System.out.println("无效指令");
                    break;
            }
        }
    }

    private void findAllTeacher() throws IOException {
        //1. 请求服务器,获取列表数据
        Socket socket = getSocket();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(Option.FINDALL_TEACHER + "&itheima");
        bw.newLine();
        bw.flush();

        //2. 接收服务器响应回来的结果
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        List<Teacher> list = null;
        try {
            list = (List<Teacher>) ois.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        ois.close();
        socket.close();


        //3. 判断系统有没有数据
        if (list == null || list.size() == 0) {
            System.out.println("系统暂无数据,请添加后重试!");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            list.forEach(teacher -> System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday()));
        }
    }

    private void updateTeacher() throws IOException {
        //获取输入的信息
        System.out.println("请输入教师工号");
        String id = sc.next();
        System.out.println("请输入教师姓名");
        String name = sc.next();
        System.out.println("请输入教师生日");
        String birthday = sc.next();
        String age = AgeUtil.getAge(birthday);
        //将其封装成teacher对象
        Teacher teacher = new Teacher(id, name, age, birthday);

        //发送请求到服务器,并打印返回结果
        System.out.println(sendMsgToServer(Option.UPDATE_TEACHER + "&" + teacher.toString()));
    }

    private void delectTeacher() throws IOException {
        System.out.println("请输入要删除的id");
        String id = sc.next();
        //发送请求到服务器,并打印返回结果
        System.out.println(sendMsgToServer(Option.DELECT_TEACHER + "&" + id));
    }

    private void addTeacher() throws IOException {
        //获取输入的信息
        System.out.println("请输入教师工号");
        String id = sc.next();
        System.out.println("请输入教师姓名");
        String name = sc.next();
        System.out.println("请输入教师生日");
        String birthday = sc.next();
        String age = AgeUtil.getAge(birthday);
        //将其封装成teacher对象
        Teacher teacher = new Teacher(id, name, age, birthday);

        //发送请求到服务器,并打印返回结果
        System.out.println(sendMsgToServer(Option.ADD_TEACHER + "&" + teacher.toString()));

    }

    //发送请求给服务器,返回服务器传回结果
    private Object sendMsgToServer(String clientMsg) throws IOException {
        System.out.println("发送请求:" + clientMsg + "给服务器");
        //发送请求给服务器,进行业务操作
        Socket socket = getSocket();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(clientMsg);
        bw.newLine();
        bw.flush();
        socket.shutdownOutput();

        //接收服务器返回数据,并展示给用户
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String resultInfo = br.readLine();
        bw.close();
        socket.close();
        return resultInfo;
    }

    //封装一个方法,专门获取socket对象
    private Socket getSocket() {
        try {
            return new Socket("127.0.0.1", 10000);
        } catch (Exception e) {
            return null;
        }
    }
}
